import {createSlice, Draft, PayloadAction} from "@reduxjs/toolkit";
import {AppState} from "@store/index";

//状态切片中的状态的类型
export interface AuthState {
    auth:Partial<Auth>
}
// 创建用于管理用户登录凭据的状态的切片
export const authSlice = createSlice<AuthState,{
    //保存用户登录凭据
    saveAuth(state : Draft<AuthState>,action:PayloadAction<Auth>) :void;
    // 清空用户登录凭据
    clearAuth(state:Draft<AuthState>):void
},"auth">({
    name:'auth',
    initialState:{
        auth : {

        },
    },
    reducers:{
        saveAuth(state: Draft<AuthState>, action: PayloadAction<Auth>) {
            state.auth = action.payload
        },
        clearAuth(state: Draft<AuthState>) {
            state.auth = {}
        }
    }
})

// 导出用于操作用户资料的action creator 函数的对象集合
export const {saveAuth,clearAuth} = authSlice.actions
// 导出用于获取用户资料的状态选择器
export const selectAuth = (state: AppState) => state.auth.auth

